package com.manager.service.impl;

import com.manager.bean.PageInfo;
import com.manager.bean.Visitor;
import com.manager.bean.VisitorExample;
import com.manager.mapper.VisitorMapper;
import com.manager.service.VisitorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Visitor)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:26
 */
@Service("visitorService")
public class VisitorServiceImpl implements VisitorService {
    @Resource
    private VisitorMapper visitorDao;


    //分页查询
    @Override
    public PageInfo<Visitor> findPageInfo(String name, String phone , Integer pageIndex, Integer pageSize) {
        PageInfo<Visitor> pi = new PageInfo<Visitor>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        VisitorExample visitorExample=new VisitorExample();
        VisitorExample.Criteria criteria=visitorExample.createCriteria();
        if(name!=null&name!=""){
            criteria.andNameLike("%"+name+"%");
        }
        if(phone!=null&phone!=""){
            criteria.andPhoneEqualTo((phone));
        }
        //获取总条数
        Integer totalCount = visitorDao.countByExample(visitorExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            visitorExample.setOrderByClause("enter+0 DESC");
            List<Visitor> visitorList =	visitorDao.selectByExample(visitorExample);
            int end=start+pi.getPageSize();
            if(end>visitorList.size()-1){
                end=visitorList.size();
            }
            visitorList=visitorList.subList(start,end);
            pi.setList(visitorList);
        }
        return pi;
    }

    //无
    @Override
    public Visitor queryById(Integer Id) {
        return visitorDao.selectByPrimaryKey(Id);
    }


    @Override
    public List<Visitor> getAll() {
        VisitorExample visitorExample = new VisitorExample();
        VisitorExample.Criteria criteria = visitorExample.createCriteria();
        return visitorDao.selectByExample(visitorExample);
    }

    /**
     * 新增数据
     *
     * @param visitor 实例对象
     * @return 实例对象
     */
    @Override
    public Visitor insert(Visitor visitor) {
        this.visitorDao.insertSelective(visitor);
        return visitor;
    }

    /**
     * 修改数据
     *
     * @param visitor 实例对象
     * @return 实例对象
     */
    @Override
    public Visitor update(Visitor visitor) {
        visitorDao.updateByPrimaryKey(visitor);
        return new Visitor();
    }

    @Override
    public boolean deleteById(Integer Id) {
        return false;
    }

}
